<?php

namespace app\order\model;
use think\db\Query;
use think\Model;
use think\facade\Db;
use app\order\logic\OrderSourceLogic;
class OrderSourceModel extends Model
{
    /**
     * 表名
     */
    public $table = 'qu_order_source';

    public function attribute_labels_export(){
        $title     = [
            'created_at'             => '日期',
            'name'                   => '订单号',
            'financial_status'       => '支付状态',
            'fulfillment_status'     => '物流状态',
            'total_price'            => '现销售额$',
            'old_total_price'        => '原现销售额$',
            'refund'                 => '争议退款',
            'freight'                => '物流成本$',
            'material'               => '采购成本$',
            'dispute'                => '争议成本$',
            'dollar'                 => '美金汇率',
            'currency'               =>  '币种',
            'tag_source'             =>  'tag_source',
            'tag_campaign'           =>  'tag_campaign',
            'tags'                   =>  '原记录标签',
            'landing_site'           =>  'landing_site',
            'refund_price'           =>  '退款',
            'skuquantity'           =>  '总件数',
        ];
        return $title;
    }


    /**
     * @desc  获取满足条件的总计数据
     * @author luckyXiong
     * @date   2024-08-08
     */
    public function pageList($params) {
        // 获取基本参数
        $page  = (isset($params['page']) && $params['page'])   ? (int)$params['page'] : 1;
        $limit = (isset($params['limit']) && $params['limit']) ? (int)$params['limit'] : 20;
        $createtime  = $params['createtime'] ?? "";
        $shop_id = $params['shop_id']??'17';
        $where       = $map = [];
        $where[]     = ['a.createtime', '>=', strtotime($createtime['0'])];
        $where[]     = ['a.createtime', '<=', strtotime($createtime['1']) + 3600*24-1];
        $where[]     = ['a.financial_status', 'in', ["partially_paid", "paid", "partially_refunded", "refunded"]];
        $where[]     = ['a.shop_id', '=',$shop_id];
        $field       = 'a.order_id,a.name,a.financial_status,a.fulfillment_status,a.created_at,a.status,
                        a.tag_source,a.tag_campaign,b.freight,b.material,b.dispute, b.total_price, b.refund,
                        a.landing_site,
                        a.firstVisit_source,
                        a.firstVisit_campaign,
                        a.lastVisit_source,
                        a.lastVisit_campaign,
                        a.landingSite,
                        a.last_source,
                        a.last_campaign,
                        a.first_source,
                        a.first_campaign,
a.tags,b.old_total_price, b.currency, b.freight_status, b.material_status, b.disputeapi_type, b.disputeapi_status, b.sku, b.estimate_freight,b.estimate_weight,b.skuquantity';

        // 查询构建
        $list  = Db::name('order_source')
            ->field($field)
            ->alias('a')
            ->leftJoin('qu_order_achievement b','b.order_id = a.order_id')
            ->where($where)
            ->limit(($page - 1) * $limit, $limit)
            ->select()
            ->toArray();
        $logic = new OrderSourceLogic();
        //获取当前月份;
        $date_info = explode('-',$createtime['0']);

        if(isset($date_info[0]) && isset($date_info[1])){
            $date      = $date_info[0] . '-' . $date_info[1];
        }else{
            $date      = '06';
        }

        $dollar    = Db::name('order_exchange')
                     ->where(['date' => $date])
                     ->value('dollar');
        try{
            foreach($list as $k=>$v){
                //填充预估物流费
                if($v['freight'] <= 0.001){
                    $v['freight'] = $v['estimate_freight'];
                }
                if(empty($v['fulfillment_status'])){
                    $list[$k]['fulfillment_status'] = "未发货";
                }
                $list[$k]['tag_source']   = $logic->tag_source($v['tag_source']);
                $list[$k]['tag_campaign'] = $logic->tag_campaign($v['tag_campaign']);
                //物流--人民币转美金
                $list[$k]['freight']  =  $v['freight'] ? getRes(($v['freight'] /$dollar)) : '0.00';
                $list[$k]['estimate_freight']  =  $v['estimate_freight'] ? getRes(($v['estimate_freight'] /$dollar)) : '0.00';
                //采购
                $list[$k]['material'] =  $v['material'] ? getRes(($v['material'] /$dollar)):'0.00';
                $list[$k]['dollar']   =  $dollar;
                if(!$v['old_total_price'] && !$v['total_price']){
                    $list[$k]['refund_price']  = '0.00';
                }else{
                    $list[$k]['refund_price']   =  $v['old_total_price']-$v['total_price'];
                }
            }

        // 总记录数
        $map[]     = ['createtime', '>=', strtotime($createtime['0'])];
        $map[]     = ['createtime', '<=', strtotime($createtime['1']) + 3600*24-1];
        $map[]     = ['shop_id', '=', $shop_id];
        $map[]     = ['financial_status', 'in',["partially_paid", "paid", "partially_refunded", "refunded"]];
        $total     = Db::name('order_source')
                 ->where($map)
                 ->count();
        // 返回结果
        return [
            'data'         => $list,
            'total'        => $total,
        ];
        }catch (\Exception $e) {
            echo $e->getMessage();
            return error('保存失败');
        }
   }
 }